我有一个关于 Shiny 和数据框使用的问题。
我想我明白我需要创建孤立的或反应性的环境来与之交互,但是如果我尝试使用 Dataframe,我会收到一条错误消息:
Error in pfData: konnte Funktion "pfData" nicht finden
我试图通过此代码操作数据框:
server <- function(input, output) {
observeEvent(input$go,
{
pf_name <- reactive({input$pfID})
pf_date <- reactive({input$pfDate})
if (pf_name()!="please select a PF") {
pfData <- reactive(read.csv(file =paste(pf_name(),".csv",sep=""),sep=";",dec=","))
MDur <- pfData()[1,15]
pfData <- pfData()[3:nrow(pfData()),]
Total = sum(pfData()$Eco.Exp...Value.long)
}
})
}
如果我在控制台中操作我的数据框,它就可以正常工作:
pfData <- pfData[3:nrow(pfData),]
Total = sum(pfData$Eco.Exp...Value.long)
Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
你能帮我吗?
编辑:
library(shiny)
ui <- fluidPage(
fluidRow(
column(6, offset =3,
wellPanel(
"Choose Mandate and Date",
fluidRow(
column(4,selectInput("pfID",label = "",
choices = list("please select a PF","GF25",
"FPM"),
selected = "please select a PF") ),
column(4, dateInput("pfDate",label="",value = Sys.Date()) ),
column(2, actionButton("go","Submit")),column(2,textOutput("selected_var"))
)
)
)
)
)
# Define server logic ----
server <- function(input, output) {
pfDataReactive <- reactive({
input$go
if (pf_name()!="please select a PF") {
pfData <- read.csv(file =paste(pf_name(),".csv",sep=""),sep=";",dec=",")
MDur <- pfData[1,15]
pfData <- pfData[3:nrow(pfData),]
Total = sum(pfData$Eco.Exp...Value.long)
Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
pfData
output$selected_var <- renderText({paste(MDur)})
}
})
}
# Run the app ----
shinyApp(ui = ui, server = server)
谢谢 斯蒂芬
答案 0 :(得分:1)
如果没有工作示例,就不可能确定您要做什么,但听起来您需要一个 reactive
而不是使用 observeEvent
。
尝试类似的东西
pfDataReactive <- reactive({
input$go
pfData <- read.csv(file =paste(pf_name(),".csv",sep=""),sep=";",dec=",")
Total = sum(pfData$Eco.Exp...Value.long)
Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
pfData
})
然后在您的 Shiny 应用程序的服务器函数中使用 pfDataReactive()
,只要您在控制台代码中引用 pfData
。
对 input$go
的独立引用确保响应式将在 input$go
更改/被点击/等时更新。
更新
您的代码仍然存在重大问题。您已将分配添加到输出对象作为我给您的 reactive
的最后一行,因此 reactive
始终返回 NULL
。这没有帮助,这也是它“根本不活跃”的原因之一......
其次,当相关输入对象似乎是 pf_name
时,您测试是否存在名为 input$pfID
的反应式/函数。这是 reactive
永远不会更新的另一个原因。
请注意我对 input$pfID
的定义所做的更改,以提高 pfDataReactive
对象的可读性。 (此更改也可能意味着您可以完全取消 input$go
。)
如您所说,我无权访问您的 csv 文件,因此我无法完全测试您的代码。我修改了 pfDataReactive
的主体,以简单地将 mtcars
数据集作为字符串返回。我还编辑了我注释掉的代码,希望在您将其与 real csv 文件一起使用时能够正确运行。
此代码似乎提供了您想要的行为。不过,如果我可以发表主观评论,我认为您的 GUI 布局令人震惊。 ;=)
library(shiny)
ui <- fluidPage(
fluidRow(
column(6, offset =3,
wellPanel(
"Choose Mandate and Date",
fluidRow(
column(4,selectInput("pfID",label = "",
# Modified to that "Pleaseselect a PF" returns NULL
choices = list("please select a PF"="","GF25", "FPM"),
selected = "please select a PF") ),
column(4, dateInput("pfDate",label="",value = Sys.Date()) ),
column(2, actionButton("go","Submit")),column(2,textOutput("selected_var"))
)
)
)
)
)
# Define server logic ----
server <- function(input, output) {
pfDataReactive <- reactive({
# Don't do anything until we have a PF csv file
req(input$pfID)
input$go
# Note the change to the creation of the file name
# pfData <- read.csv(file =paste(input$pfID,".csv",sep=""),sep=";",dec=",")
# pfData <- pfData[3:nrow(pfData),]
# Total = sum(pfData$Eco.Exp...Value.long)
# Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
# pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
# MDur <- pfData[1,15]
# If you want to print MDur in the selected_var output, MDur should be the retrun value from this reactive
# MDur
mtcars
})
output$selected_var <- renderText({
print("Yep!")
as.character(pfDataReactive())
})
}
# Run the app ----
shinyApp(ui = ui, server = server)
下一次,拜托,请更加努力地提供 MWE。 This post 可能会有所帮助。
This 很好地介绍了 Shiny。