我正在尝试使用expr
创建一个动画,以显示跑步者在两个连续圈之间的速度如何随时间变化。我尝试使用以下代码将图传递到MakeSpLaps <- function(finishers.pace, lap1, lap2, start.lap) {
sp <- qplot(lap1, lap2, data=finishers.pace,
color=gender, alpha = I(.7) )
# + additional elements removed;
return(sp)
}
MakeSpLapsAnimation <- function(){
brk <- seq(0, 3000, 60)
lbl <- seconds_to_period(brk)
oopt = ani.options(interval = 0.2, nmax = 20)
saveHTML({
par(mar = c(4, 4, 0.5, 0.5))
for (i in 3:11){
# The problematic line below
MakeSpLaps(p, p[[i]], p[[i+1]], i-2)
ani.pause()
}
}, img.name = "lap_plot", imgdir = "lap_dir", htmlfile = "laps.html",
autobrowse = FALSE, title = "Plots of consecutive laps.",
description = "Plots of consecutive laps.")
}
块:
p
data.frame 'data.frame': 17051 obs. of 11 variables:
$ bib : int 10001 10003 10004 10005 10006 10009 10010 10011 10012 10013 ...
$ gender : Factor w/ 3 levels "","F","M": 3 3 3 3 3 3 3 3 3 3 ...
$ X5km_lap : num 290 204 196 315 228 ...
$ X10km_lap : num 280 204 201 322 225 ...
$ X15km_lap : num 283 205 204 326 235 ...
$ X20km_lap : num 282 206 204 342 229 ...
$ X25km_lap : num 280 210 205 371 235 ...
$ X30km_lap : num 280 225 216 407 254 ...
$ X35km_lap : num 279 274 231 404 267 ...
$ X40km_lap : num 284 251 257 357 262 ...
$ Finish_lap: num 289 242 247 333 265 ...
的位置如下:
MakeSpLaps(p, p[[3]], p[[4]], 1)
单独运行saveHTML()
会创建我想要的图表,但是当我将其插入saveHTML()
时,除了空白的PNG之外,没有创建任何图表。使用以下警告创建HTML文件。如何正确地将图表传递给函数animation option 'nmax' changed: 20 --> 1
animation option 'nmax' changed: 1 --> 20
HTML file created at: laps.html
?
You can use Thread and handler as below example shows.
public class ThreadHandlerAndroidExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_example_thread_handler);
final Button GetServerData = (Button) findViewById(R.id.GetServerData);
// On button click call this listener
GetServerData.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(),
"Please wait, connecting to server.",
Toast.LENGTH_SHORT).show();
// Create Inner Thread Class
Thread background = new Thread(new Runnable() {
private final HttpClient Client = new DefaultHttpClient();
private String URL = "http://androidexample.com/media/webservice/getPage.php";
// After call for background.start this run method call
public void run() {
try {
String SetServerString = "";
HttpGet httpget = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
SetServerString = Client.execute(httpget, responseHandler);
threadMsg(SetServerString);
} catch (Throwable t) {
// just end the background thread
Log.i("Animation", "Thread exception " + t);
}
}
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
// Define the Handler that receives messages from the thread and update the progress
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(
getBaseContext(),
"Server Response: "+aResponse,
Toast.LENGTH_SHORT).show();
}
else
{
// ALERT MESSAGE
Toast.makeText(
getBaseContext(),
"Not Got Response From Server.",
Toast.LENGTH_SHORT).show();
}
}
};
});
// Start Thread
background.start(); //After call start method thread called run Method
}
});
}
}
实际代码在这里:https://github.com/hktang/rscraper/blob/3d542b18b5f6fbf1a1fa31b0bd3936f1179cdc59/r/visuals.R#L145