- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我的代码。它可能看起来有点长,但实际上它是一个非常简单的应用程序。
用户应该上传一个很小的数据框(如果您在美国,则为 x.csv,如果您在欧洲,则为 x_Europe.csv)。然后用户应该点击按钮开始计算。最后,用户应该能够将这些计算的结果下载为数据框。
我的问题:上传文件后,当我单击“do_it”操作按钮时 - 没有任何 react 。我可以看到它,因为我的控制台没有打印任何内容。为什么?毕竟,我的函数“main_calc”应该是 input$do_it 的 eventReactive?为什么 main_calc 中的所有计算仅在用户尝试下载结果后才开始发生?
重要提示:将“数据”函数与 main_calc 分开对我来说很重要。
非常感谢!
首先,在您的工作目录中生成以下 2 个文件之一:
# generate file 'x.csv' to read in later in the app:
write.csv(data.frame(a = 1:4, b = 2:5), "x.csv", row.names = F) # US file
write.csv2(data.frame(a = 1:4, b = 2:5), "x_Europe.csv", row.names = F)
library(shiny)
ui <- fluidPage(
# User should upload file x here:
fileInput("file_x", label = h5("Upload file 'x.csv'!")),
br(),
actionButton("do_it", "Click Here First:"),
br(),
br(),
textInput("user_filename","Save your file as:", value = "My file x"),
downloadButton('file_down',"Save the output File:")
)
server <- function(input, output, session) {
#----------------------------------------------------------------------
# Function to read in either European (csv2) or American (csv) input:
#----------------------------------------------------------------------
ReadFile <- function(pathtofile, withheader = TRUE){
test <- readLines(pathtofile, n = 1)
if (length(strsplit(test, split = ";")[[1]]) > 1) {
print("Reading European CSV file")
outlist <- list(myinput = read.csv2(pathtofile, header = TRUE),
europe.file = 1)
} else {
print("Reading US CSV file")
outlist <- list(myinput = read.csv(pathtofile, header = TRUE),
europe.file = 0)
}
return(outlist)
}
#----------------------------------------------------------------------
# Data-related - getting the input file
#----------------------------------------------------------------------
Data <- reactive({
print("Starting reactive function 'Data'")
# Input file:
infile_x <- input$file_x
myx <- ReadFile(infile_x$datapath)$myinput
# European file?
europe <- ReadFile(infile_x$datapath)$europe.file
print("Finishing reactive function 'Data'")
return(list(data = myx, europe = europe))
})
#----------------------------------------------------------------------
# Main function that should read in the input and 'calculate' stuff
# after the users clicks on the button 'do_it' - takes about 20 sec
#----------------------------------------------------------------------
main_calc <- eventReactive(input$do_it, {
req(input$file_x)
# Reading in the input file:
x <- Data()$data
print("Done reading in the data inside main_calc")
# Running useless calculations - just to kill time:
myvector <- matrix(unlist(x), ncol = 1, nrow = 1000)
print("Starting calculations")
for (i in seq_len(10)) {
set.seed(12)
mymatr <- matrix(abs(rnorm(1000000)), nrow = 1000)
temp <- solve(mymatr) %*% myvector
}
print("Finished calculations")
# Creating a new file:
y <- temp
result = list(x = x, y = y)
print("End of eventReactive function main_calc.")
return(result)
}) # end of main_calc
#----------------------------------------------------------------------
# The user should be able to save the output of main_calc as a csv file
# using a string s/he specified for the file name:
#----------------------------------------------------------------------
output$file_down <- downloadHandler(
filename = function() {
paste0(input$user_filename, " ", Sys.Date(), ".csv")
},
content = function(file) {
print("Europe Flag is:")
print(Data()$europe)
if (Data()$europe == 1) {
x_out <- main_calc()$x
print("Dimensions of x in downloadHandler are:")
print(dim(x_out))
write.csv2(x_out,
file,
row.names = FALSE)
} else {
x_out <- main_calc()$x
print("Dimensions of x in downloadHandler are:")
print(dim(x_out))
write.csv(x_out,
file,
row.names = FALSE)
}
}
)
} # end of server code
shinyApp(ui, server)
最佳答案
以下是解决方案 - 基于 MrFlick 的建议:
# generate file 'x.csv' to read in later in the app:
# write.csv(data.frame(a = 1:4, b = 2:5), "x.csv", row.names = F)
# write.csv2(data.frame(a = 1:4, b = 2:5), "x_Europe.csv", row.names = F)
library(shiny)
library(shinyjs)
ui <- fluidPage(
# User should upload file x here:
fileInput("file_x", label = h5("Upload file 'x.csv'!")),
br(),
actionButton("do_it", "Click Here First:"),
br(),
br(),
textInput("user_filename","Save your file as:", value = "My file x"),
downloadButton('file_down',"Save the output File:")
)
server <- function(input, output, session) {
#----------------------------------------------------------------------
# Function to read in either European (csv2) or American (csv) input:
#----------------------------------------------------------------------
ReadFile <- function(pathtofile, withheader = TRUE){
test <- readLines(pathtofile, n = 1)
if (length(strsplit(test, split = ";")[[1]]) > 1) {
print("Reading European CSV file")
outlist <- list(myinput = read.csv2(pathtofile, header = TRUE),
europe.file = 1)
} else {
print("Reading US CSV file")
outlist <- list(myinput = read.csv(pathtofile, header = TRUE),
europe.file = 0)
}
return(outlist)
}
#----------------------------------------------------------------------
# Data-related - getting the input file
#----------------------------------------------------------------------
Data <- reactive({
print("Starting reactive function Data")
# Input file:
infile_x <- input$file_x
myx <- ReadFile(infile_x$datapath)$myinput
# European file?
europe <- ReadFile(infile_x$datapath)$europe.file
print("Finishing reactive function 'Data'")
return(list(data = myx, europe = europe))
})
#----------------------------------------------------------------------
# Main function that should read in the input and 'calculate' stuff
# after the users clicks on the button 'do_it' - takes about 20 sec
#----------------------------------------------------------------------
# Creating reactive Values:
forout_reactive <- reactiveValues()
observeEvent(input$do_it, {
print("STARTING observeEvent")
req(input$file_x)
# Reading in the input file:
x <- Data()$data
print("Done reading in the data inside observeEvent")
# Running useless calculations - just to kill time:
myvector <- matrix(unlist(x), ncol = 1, nrow = 1000)
print("Starting calculations")
for (i in seq_len(10)) {
set.seed(12)
mymatr <- matrix(abs(rnorm(1000000)), nrow = 1000)
temp <- solve(mymatr) %*% myvector
} # takes about 22 sec on a laptop
print("Finished calculations")
# Creating a new file:
y <- temp
forout_reactive$x = x
forout_reactive$y = y
print("End of observeEvent")
}) # end of main_calc
#----------------------------------------------------------------------
# The user should be able to save the output of main_calc as a csv file
# using a string s/he specified for the file name:
#----------------------------------------------------------------------
output$file_down <- downloadHandler(
filename = function() {
paste0(input$user_filename, " ", Sys.Date(), ".csv")
},
content = function(file) {
print("Europe Flag is:")
print(Data()$europe)
if (Data()$europe == 1) {
y_out <- forout_reactive$y
print("Dimensions of y in downloadHandler are:")
print(dim(y_out))
write.csv2(y_out,
file,
row.names = FALSE)
} else {
y_out <- forout_reactive$y
print("Dimensions of y in downloadHandler are:")
print(dim(y_out))
write.csv(y_out,
file,
row.names = FALSE)
}
}
)
} # end of server code
shinyApp(ui, server)
关于r Shiny : eventReactive is not reacting when the button is pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910473/
我在按下按钮后制作了一个扩展表单,但现在我也想添加 Enter 按键,但我不知道如何操作。也许有人可以帮助我!? 代码 $(document).ready( function() {
基本上我使用的是 z-index,通过单击按钮,它应该拍摄背面照片并将其置于正面,反之亦然。我目前正试图让按钮正常工作。 Lab 5, Part 1
这个问题已经有答案了: AlertDialog OnBackPressed() Not Working Properly (3 个回答) 已关闭 4 年前。 我想实现抽屉导航后按对话框上的对话框后按。
我有一个点击功能,长按同一个按钮。实现了长按事件,但我需要分别找到按钮 UP_EVENT 和 DOWN_EVENTS。如何使用 OnLongClickListener 实现 View.OnLongC
我需要简单地检测每时每刻在我的 Qt5/C++ 应用程序中何时按下或释放右键。 到目前为止,我已经编写了这些函数: void test::mousePressEvent(QMouseEvent *ev
我有一个输入框和它下面的“添加”按钮。添加按钮基本上所做的是,它将一个新的输入字段附加到文档中。我在这里尝试做的是,在输入字段中键入时,如果检测到返回键按下,调用添加新输入字段的函数将被触发,与单击添
我正在使用 Java swing 和 Matlab 开发一个项目。我有一个带有 2 个按钮“运行”和“暂停”的 GUI。我正在使用另外一个 java 程序 (Matlabjavaprog.java),
目前,我有一个带有 9 补丁图像作为边框的自定义 View 。 该自定义 View 在 LinearLayout 中放置了三次,所以它看起来像这样: +------------------------
我正打算用一个 TButtonedEdit 控件替换 TEdit + TButton 组合,但当我尝试测试它时,我发现无法使用键盘“按下”(右)按钮。 我尝试了 Alt+Enter、Alt+Down、
您好,我在模糊()和回车键上执行相同的代码时遇到了一些麻烦。我使用以下代码: $(".formatted-date").bind('blur keyup',function(e) { if
这是一个关于android java编程的一般问题。 有没有办法(当手机锁定时)检测是否按下主页按钮几秒钟? 最佳答案 由于不需要硬件按钮,因此并非所有手机都可以使用主页。 关于java - 手机锁定
我在 Pane 中有一个 ImageView,并且该 Pane 已设置 MouseEventHandler 来执行一些操作。 ImageView 的图像在 CSS 文件中定义如下: .imageVie
我需要以编程方式从交互功能内部按下一个键。以下是我目前所掌握的内容的概要: (defun answer-to-life-the-universe-and-everything () (intera
我正在尝试做一个简单的选择器。 这是我的选择器 xml 代码,名为“butt.xml”: 这就是布局: 这就是代码: ImageButton ib; @Override public vo
基本上我是在调用 API 来检索慈善机构列表。然后我将所有这些放在一个数组中并动态设置 UIButtons。 然后我允许用户选择慈善机构并显示包含该索引数据的 View 。 我的循环在这里; for
让我解释更多细节。 首先;我知道如果用户按回键并返回上一个 Activity ,则上一个 Activity 会正确触发 onResume 方法。这没关系。 我的应用程序中有一个根 Activity ,
我正在使用 BootStrap Group Button 类,发现在我自己的代码中,如果按下一个按钮,该按钮将在几秒钟后弹出.... 如何确保它保持按下状态? Stage
我有一个 ListView,当通过 onItemClick 监听器单击项目行时,它会打开另一个 Activity 。 我希望该行从被点击到屏幕切换到新 Activity 时一直保持按下状态。我认为这对
我正在使用 Hammer.JS 来检查事件。我想长按做一些事情。所以我正在使用像这样的锤子新闻事件 var mc = new Hammer(can); mc.on("press", function
我构建了一个 Swing GUI 应用程序,除了一些细节之外,一切都运行良好:我有两个按钮,每个按钮都附加了一个鼠标单击事件。问题是,当我单击它们时,它们保持“按下”状态:其他一切都正常,但看到这两个
我是一名优秀的程序员,十分优秀!