用户输入numericInput有光泽

时间:2015-11-09 06:24:28

标签: r shiny

我在更复杂的设置上实现此代码时遇到了困难。我的目标是让用户使用numericInput按钮渲染的投影值更改列值。 Global.R不是一个选项,因为在达到最终版本之前,数据集上有许多操作。我有一切设置,除了能够观察输入$动作并在动作发生时更改数据帧。

ui.R

shinyUI(
fluidPage(
titlePanel("Basic DataTable"),



# Create a new row for the table.
 fluidRow(
  selectInput("select", label = h3("Select box"), 
              choices = unique(data$Projection), 
              selected = unique(data$Projection)[1]),
  numericInput("num", label = h3("Numeric input"), value = unique(data$Projection)[1]),
  submitButton(text = "Apply Changes", icon = NULL),
  dataTableOutput(outputId="table")
)    
)  
)

server.R

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.

 # Given that you are not plotting this line is
### useless library(ggplot2)

# Define a server for the Shiny app

shinyServer(function(input, output) {

  # Filter data based on selections
  output$table <- renderDataTable({
    data$User_Prediction[data$Projection==input$select] <<- input$num
    data
  })
})

global.r

 data <- as.data.frame(c(98,99,34))
 names(data) <- "Projection"
 data$User_Prediction <- 0

数据存储在server.R中

 data <-reactive({
 ...
 ...
 ...
 data
 })

我正在尝试将存储在反应函数中的数据引入

 d <- reactiveValues(dat=data)

我试过了

 d <- data()
 d <- data
 d <- reactiveValues(dat=data)

我有什么方法可以访问数据,因为否则你的代码会工作。

1 个答案:

答案 0 :(得分:2)

如果你想在没有observeEvent的情况下这样做,你可以这样做:

data <- as.data.frame(c(98,99,34))
names(data) <- "Projection"
data$User_Prediction <- 0

ui <- shinyUI(
  fluidPage(
    titlePanel("Basic DataTable"),



    # Create a new row for the table.
    fluidRow(
      column(12,
             selectInput("select", label = h3("Select box"), 
                         choices = unique(data$Projection), 
                         selected = unique(data$Projection)[1]),
             numericInput("num", label = h3("Numeric input"), value = unique(data$Projection)[1]),
             actionButton('btn',"Apply Changes"),
             dataTableOutput(outputId="table")
             )
    )    
  )  
)

server <- shinyServer(function(input, output) {
  d <- reactive({
    data
  })

  dat <- reactiveValues(dat=NULL)
  observe({
    dat$dat <- d()
  })

  observe({
    input$btn
    isolate({
      num <- input$num
      sel <- input$select
    })
    dat$dat$User_Prediction[dat$dat$Projection==sel] <- num
    #d2 <- dat
  })

  # Better way
#   observeEvent(input$btn,{
#     dat$dat$User_Prediction[dat$dat$Projection==sel] <- num
#   })

  # Filter data based on selections
  output$table <- renderDataTable({
    dat$dat
  })
})

shinyApp(ui=ui,server=server)