我刚刚开始尝试Shiny。我试图做的事情看起来很简单,但我做对了。
我只想获得两个用逗号分隔的值字符串,然后,当按下操作按钮“ go”时,将这些字符串转换为数字矢量,并绘制另一个字符串。
问题似乎出在字符串到数字矢量的转换上。在R中,我通常使用以下行并可以正常工作
x<-"2,4,6,98"
y<-as.numeric(unlist(strsplit(x,",")))
y
[1] 2 4 6 98
但是当“ eventReactive”中包含相同的内容时,我会得到
as.double中的错误:无法将类型'closure'强制转换为类型'double'的向量
这是我尝试的代码:
library(shiny)
ui <- fluidPage(
titlePanel("Plot data"),
sidebarPanel(
textInput(inputId = "Conc",label="measured concentrations",value="",
placeholder ="Enter a numeric vector, comma separated"),
textInput(inputId = "Time",label="Time", value="",
placeholder ="Enter a numeric vector, comma separated"),
actionButton("go","Go!")
),
mainPanel(
plotOutput(outputId = "plot1")
)
)
server<-function(input, output){
t<-eventReactive(input$go,{as.numeric(unlist(strsplit(input$Time,",")))})
parent<-eventReactive(input$go,{as.numeric(unlist(strsplit(input$Conc,",")))})
pp<-reactive({plot(t,parent)})
output$plot1 <- renderPlot({pp()})
}
shinyApp(ui=ui,server=server)
感谢您的帮助!
答案 0 :(得分:0)
无法理解您想要什么,但请尝试以下操作:
library(shiny)
ui <- fluidPage(
titlePanel("Plot data"),
sidebarPanel(
textInput(inputId = "Conc",label="measured concentrations",value="",
placeholder ="Enter a numeric vector, comma separated"),
textInput(inputId = "Time",label="Time", value="",
placeholder ="Enter a numeric vector, comma separated"),
actionButton("go","Go!")
),
mainPanel(
plotOutput(outputId = "plot1")
)
)
server<-function(input, output){
val<-reactiveValues()
observeEvent(input$go,{
val$t<-as.numeric(unlist(strsplit(input$Time,",")))
val$parent<-as.numeric(unlist(strsplit(input$Conc,",")))
})
output$plot1 <- renderPlot({
validate(need(val$t,""))
validate(need(val$parent,""))
plot(val$t,val$parent)
})
}
shinyApp(ui=ui,server=server)